<template>
  <view class="index">
    <view class="jtop">
      <view class="status_bar"><!-- 这里是状态栏 --></view>
      <view class="jtop_l" @tap="navigateBack"
        ><image src="../../static/img/ic_back.png"></image
      ></view>
      <!-- <view class="jtop_l" @tap="navigateBack">关闭</view> -->
      <view class="jtop_c ell">修改{{ jc.name }}采购计划批次</view>
      <view
        class="jtop_r"
        :style="{ opacity: batchRequestList[0].param ? '1' : '.6' }"
        @click="jsave"
        >确定</view
      >
    </view>
    <view class="tophr"></view>

    <view class="addb">
      <view class="addi2">
        <text>材料类别</text>
        <input
          type="text"
          placeholder="请输入材料类别(必填)"
          :value="jc.category"
          @input="onKeyInput_category"
          placeholder-style="color:#CCCCCC"
          disabled
        />
      </view>
      <view class="addi2 ">
        <text>材料名称</text>
        <input
          type="text"
          placeholder="请输入材料名称(必填)"
          :value="jc.name"
          @input="onKeyInput_name"
          placeholder-style="color:#CCCCCC"
          disabled
        />
      </view>
      <view class="addi2 noborder">
        <text>材料单位</text>
        <input
          type="text"
          placeholder="请输入材料单位(必填)"
          :value="jc.unit"
          @input="onKeyInput_unit"
          placeholder-style="color:#CCCCCC"
          disabled
        />
      </view>
    </view>

    <view
      class="addb"
      v-for="(item, index) in batchRequestList.length"
      :key="index"
      :id="index"
      @tap="editlist(index)"
    >
      <view class="addi2">
        <text>主要规格参数</text>
        <input
          type="text"
          placeholder="请输入主要参数(必填)"
          :value="batchRequestList[index].param"
          @input="onKeyInput_param($event, index)"
          placeholder-style="color:#CCCCCC"
        />
      </view>

      <view class="addi2">
        <view class="">
          <text>数量</text>
          <input
            type="number"
            placeholder="请输入采购数量"
            :value="batchRequestList[index].amount"
            @input="onKeyInput_amount($event, index)"
            placeholder-style="color:#CCCCCC"
          />
        </view>

        <!-- <view class="addi21">
          <text>单位</text>

          <input
            type="text"
            placeholder="请输入单位"
            :value="batchRequestList[index].unit"
            @input="onKeyInput_unit($event, index)"
            placeholder-style="color:#CCCCCC"
          />
        </view> -->
      </view>

      <view class="addi2">
        <text>预算单价(元)</text>
        <input
          type="text"
          placeholder="请输入预算单价"
          :value="batchRequestList[index].price"
          @input="onKeyInput_price($event, index)"
          placeholder-style="color:#CCCCCC"
        />
      </view>
      <view class="addi2">
        <text>预算总价(元)</text>
        <input
          type="text"
          placeholder=""
          :value="batchRequestList[index].total_price"
          disabled
          placeholder-style="color:#CCCCCC"
        />
      </view>

      <e-picker-plus
        endRule="2040-12-31"
        class="addbi"
        @confirm="confirm($event, index)"
        mode="YMD"
      >
        <text class="addbil">最晚到货时间</text>
        <image
          src="../../static/img/jt.png"
          @confirm="confirm($event, index)"
        ></image>
        <text class="addbir">{{ batchRequestList[index].lastInTime }}</text>
      </e-picker-plus>

      <view class="addi2 noborder">
        <text>采购计划编号</text>
        <input
          type="text"
          placeholder="请输入采购计划编号	"
          :value="batchRequestList[index].purchasePlanId"
          @input="onKeyInput_purchasePlanId($event, index)"
          placeholder-style="color:#CCCCCC"
        />
      </view>
    </view>

    <view class="ptt">
      <view class="ptft" @tap="add_jbox">
        <image src="../../static/img/index/5/xx4.png"></image>
        <text>添加材料批次</text>
      </view>
    </view>
  </view>
</template>

<script>
import request from '@/common/request.js';
import { mapState } from 'vuex';

export default {
  data() {
    return {
      repeat_click: true,
      ashowpop: false,
      ashowpop_jrename: false,
      ashowpop_jdel: false,
      ashowpopT: false,
      bj: false,
      yd: false,
      lastInTime: '',
      remark: '',
      amount: '',
      model: '',
      brand: '',
      name: '',
      index: 0,
      array: [],
      array2: [],
      // total_price: '0.00',

      batchRequestList: [
        {
          amount: '',
          lastInTime: '',
          param: '',
          price: '',
          purchasePlanId: '',

          total_price: '0.00',
        },
      ],

      category: '',
      name: '',
      unit: '',
      jc: uni.getStorageSync('jc'),
      // array: ['中国', '美国', '巴西', '日本'],
    };
  },

  methods: {
    bindPickerChange: function(e) {
      console.log('picker发送选择改变，携带值为', e.target.value);
      this.index = e.target.value;
    },

    add_jbox() {
      this.batchRequestList.push({
        amount: '',
        lastInTime: '',
        param: '',
        price: '',
        purchasePlanId: '',
        total_price: '0.00',
      });
      console.log('1212', this.batchRequestList);
    },

    confirm(e, a) {
      console.log(e, a);
      this.batchRequestList[a].lastInTime = e.result;
    },

    onKeyInput_amount: function(event, id) {
      this.batchRequestList[id].amount = event.detail.value;
      if (this.batchRequestList[id].amount && this.batchRequestList[id].price) {
        this.batchRequestList[id].total_price =
          parseFloat(this.batchRequestList[id].amount) *
          parseFloat(this.batchRequestList[id].price);
      } else {
        this.batchRequestList[id].total_price = '0.00';
      }
    },

    onKeyInput_param: function(event, id) {
      this.batchRequestList[id].param = event.detail.value;
    },

    onKeyInput_price: function(event, id) {
      this.batchRequestList[id].price = event.detail.value;
      if (this.batchRequestList[id].amount && this.batchRequestList[id].price) {
        this.batchRequestList[id].total_price =
          parseFloat(this.batchRequestList[id].amount) *
          parseFloat(this.batchRequestList[id].price);
      } else {
        this.batchRequestList[id].total_price = '0.00';
      }
    },

    onKeyInput_purchasePlanId: function(event, id) {
      this.batchRequestList[id].purchasePlanId = event.detail.value;
    },
    onKeyInput_unit: function(event, id) {
      this.unit = event.detail.value;
    },

    onKeyInput_category: function(event, id) {
      this.category = event.detail.value;
    },

    onKeyInput_name: function(event, id) {
      this.name = event.detail.value;
    },

    jsave() {
      if (!this.repeat_click) {
        return;
      }
      this.repeat_click = false;
      // if (!this.name) {
      //   this.repeat_click = true;
      //   uni.showToast({
      //     title: '请输入材料名称',
      //     icon: 'none',
      //   });
      //   return;
      // }
      // if(!this.brand){
      // 						this.repeat_click = true;
      //	uni.showToast({
      // 		title: '请输入材料品牌',
      // 		icon: 'none'
      // 	})
      // 	return
      // }

      // if(!this.model){
      // 						this.repeat_click = true;
      //					uni.showToast({
      // 		title: '请输入型号/规格',
      // 		icon: 'none'
      // 	})
      // 	return
      // }

      // if(!this.amount){
      // 						this.repeat_click = true;
      //uni.showToast({
      // 		title: '请输入采购数量',
      // 		icon: 'none'
      // 	})
      // 	return
      // }

      // if(!this.remark){
      // 						this.repeat_click = true;
      //	uni.showToast({
      // 		title: '请输入备注',
      // 		icon: 'none'
      // 	})
      // 	return
      // }

      // if(!this.lastInTime){
      // 						this.repeat_click = true;
      //	uni.showToast({
      // 		title: '请输入进场时间',
      // 		icon: 'none'
      // 	})
      // 	return
      // }

      request({
        url: `/biz/purchase-plan-batch/batch`,

        data: {
          purchasePlanId: this.jc.id,

          batchRequestList: this.batchRequestList,
        },

        method: 'POST',
        success: openIdRes => {
          console.log(openIdRes);
          if (openIdRes.data.success == true) {
            wx.showToast({
              title: '添加成功',
              icon: 'none',
              duration: 1000,
              success: function() {
                setTimeout(function() {
                  uni.navigateBack();
                }, 1500);
              },
            });

            // uni.navigateBack()
          } else {
            this.repeat_click = true;
            uni.showToast({
              title: openIdRes.data.msg,
              icon: 'none',
            });
            this.isLoading = false;
          }
        },
        fail: () => {
          this.repeat_click = true;
          uni.showToast({
            title: '服务器开小差，请稍后~',
            icon: 'none',
          });
          return false;
        },
      });
    },

    // 跳转
    jshow_pop() {
      this.ashowpop = !this.ashowpop;
    },
    btTC2() {
      uni.navigateTo({
        url: '../main/work_add',
      });
    },

    navigateBack() {
      uni.navigateBack();
    },
  },
  computed: mapState(['forcedLogin', 'hasLogin', 'userName']),
  onLoad() {
    // request({
    //   url: `/modor/v1/common/uom`,
    //   data: {},
    //   method: 'GET',
    //   success: openIdRes => {
    //     if (openIdRes.data.success == true) {
    //       var ss = openIdRes.data.data;
    //       ss = JSON.stringify(ss);
    //       ss = ss.substr(1);
    //       ss = ss.substr(0, ss.length - 1);
    //       ss = ss.replace(/"/g, '');
    //       ss = ss.replace(/"/g, '');
    //       ss = ss.split(',');
    //       var arr = [];
    //       var arr2 = [];
    //       for (var i = 0; i < ss.length; i++) {
    //         var c2 = ss[i];
    //         var str_before = c2.split(':')[0];
    //         var str_after = c2.split(':')[1];
    //         arr2.push({ value: str_before, label: str_after });
    //       }
    //       for (var i = 0; i < ss.length; i++) {
    //         var c2 = ss[i];
    //         var str_before = c2.split(':')[0];
    //         var str_after = c2.split(':')[1];
    //         arr.push(str_after);
    //       }
    //       this.array = arr;
    //       this.array2 = arr2;
    //       console.log(this.array);
    //       // uni.navigateBack()
    //     } else {
    //       this.repeat_click = true;
    //       uni.showToast({
    //         title: openIdRes.data.msg,
    //         icon: 'none',
    //       });
    //       this.isLoading = false;
    //     }
    //   },
    //   fail: () => {
    //     this.repeat_click = true;
    //     uni.showToast({
    //       title: '服务器开小差，请稍后~',
    //       icon: 'none',
    //     });
    //     return false;
    //   },
    // });
  },
};
</script>

<style>
.tophr {
  width: 100%;
  height: 88rpx;
}
.uni-searchbar {
  box-shadow: none;
}

.ptt {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  margin: 20rpx 0 0;
}
.pttl {
  width: 84rpx;
  height: 84rpx;
  display: block;
  float: left;
  margin: 18rpx 20rpx 18rpx 30rpx;
}
.pttr {
  width: 400rpx;
  height: 84rpx;
  display: block;
  float: left;
  margin: 18rpx 0rpx 0rpx 0rpx;
}
.pttrt {
  width: 100%;
  height: 42rpx;
  line-height: 42rpx;
  display: block;
  float: left;
  font-size: 34rpx;
  color: #000000;
}
.pttrf {
  width: 100%;
  height: 42rpx;
  line-height: 42rpx;
  display: block;
  float: left;
  font-size: 24rpx;
  color: #999999;
}
.pttjt {
  width: 16rpx;
  height: 25rpx;
  display: block;
  float: right;
  margin: 48rpx 30rpx 0 30rpx;
}
.ptf {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  margin: 20rpx 0 0;
}
.ptft {
  width: 750rpx;
  margin: 0 0 0 0rpx;
  line-height: 90rpx;
  text-align: center;
  height: 90rpx;
  display: block;
  float: left;
  background: #ffffff;
}
.ptft image {
  width: 48rpx;
  height: 48rpx;
  display: inline-block;
  float: none;
  margin: 0 10rpx -10rpx 0;
}
.ptft text {
  width: auto;
  height: 48rpx;
  display: inline-block;
  float: none;
  font-size: 34rpx;
  line-height: 48rpx;
  color: #000000;
}

.addi2 {
  width: 720rpx;
  margin: 0 0 0 30rpx;
  height: 90rpx;
  display: block;
  float: left;
  background: #ffffff;
  border-bottom: 1px solid #dddddd;
}
.addi {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  box-shadow: inset 0px 1px 0px #dddddd, inset 0px -1px 0px #dddddd;
  margin: 20rpx 0 0;
}
.addi2 text {
  width: 250rpx;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  font-size: 34rpx;
  color: #000000;
  margin: 0 0 0 0rpx;
}
.addi text {
  width: 250rpx;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  font-size: 34rpx;
  color: #000000;
  margin: 0 0 0 30rpx;
}
.addi input,
.addi2 input,
.addi2 .qt {
  width: auto;
  height: 50rpx;
  display: block;
  float: left;
  margin: 20rpx 30rpx 0 0;
  text-align: left;
  line-height: 50rpx;
  font-size: 34rpx;
  color: #000000;
}
.addb {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  margin: 20rpx 0 0;
}
.addbi {
  width: 720rpx;
  margin: 0 0 0 30rpx;
  height: 90rpx;
  display: block;
  float: left;
  background: #ffffff;
  border-bottom: 1px solid #dddddd;
}
.addb .addbi:last-child {
  border: none;
}
.addbi .addbil {
  width: 250rpx;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  font-size: 34rpx;
  color: #000000;
}
.addbi .addbir {
  width: auto;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  font-size: 32rpx;
  color: #000000;
}
.addbi image {
  width: 16rpx;
  height: 25rpx;
  display: block;
  float: right;
  margin: 33rpx 30rpx 0 30rpx;
}
.addi textarea {
  width: 690rpx;
  height: 150rpx;
  display: block;
  float: left;
  margin: 0rpx 30rpx 20rpx;
  text-align: left;
  line-height: 40rpx;
  font-size: 34rpx;
  color: #000000;
}

.index {
  background: rgb(242, 242, 242);
  width: 100%;
  height: auto;
}
.jtop {
  width: 100%;
  height: 88rpx;
  background: #ffffff;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  box-shadow: inset 0px -1px 0px #dddddd;
}
.jtop_l {
  width: auto;
  height: 88rpx;
  display: block;
  float: left;
  font-size: 32rpx;
  color: #000000;
  line-height: 88rpx;
  margin: 0 0 0 30rpx;
}
.jtop_l image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
  margin: 20rpx 0 20rpx 0;
}
.jtop_lt {
  margin: 0 0 0 10rpx;
}
.jtop_r {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: right;
  margin: 20rpx 20rpx 0 0;
}
.jtop_rl {
  margin: 20rpx 30rpx 0 0;
}
.jtop_r image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
}
.jtop_c {
  width: 70%;
  height: 48rpx;
  line-height: 48rpx;
  display: block;
  float: left;
  position: fixed;
  top: 20rpx;
  left: 15%;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
}
.jtop_c image {
  width: 36rpx;
  height: 36rpx;
  display: inline-block;
  float: none;
  margin-bottom: -5rpx;
  margin-left: 5rpx;
}
.jtop_b {
  width: 100%;
  height: auto;
  background: #ffffff;
  position: fixed;
  top: 88rpx;
  left: 0;
  z-index: 1001;
}
.jtop_r {
  width: auto;
  height: 88rpx;
  display: block;
  float: right;
  margin: 0rpx 20rpx 0 0;
  line-height: 88rpx;
  font-size: 32rpx;
  color: #000000;
  color: #256aff;
}
.doc_b22 {
  opacity: 0.5;
}

.nd {
  width: 735rpx;
  height: auto;
  display: block;
  float: left;
  margin: 0 0 0 0rpx;
}
.nd_bt {
  width: auto;
  height: 48rpx;
  display: block;
  float: right;
  border: 1px solid #dddddd;
  border-radius: 24rpx;
  margin: 40rpx 0 40rpx 30rpx;
}
.nd_bt text {
  width: auto;
  height: 48rpx;
  display: block;
  float: left;
  margin: 0 7rpx 0 18rpx;
  line-height: 48rpx;
  color: #666666;
  font-size: 24rpx;
}
.nd_bt image {
  width: 13rpx;
  height: 8rpx;
  display: block;
  float: left;
  margin: 21rpx 14rpx 0 0;
}

.tc {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10000;
  display: block;
}
.jtop_bj {
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10000;
}
.tc_fbox_bt {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  display: block;
  float: left;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
  border-top: 20rpx solid #f2f2f2;
  background: #ffffff;
}
.tc_fbox {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #f2f2f2;
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 10001;
}
.tc_fbox_bt2 {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  display: block;
  float: left;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
  border-top: 0rpx solid #f2f2f2;
  background: #ffffff;
}
.tc_fbox_bt3 {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  display: block;
  float: left;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
  border-bottom: 1px solid #dddddd;
  background: #ffffff;
}

.addi2 .qt {
  width: 50%;
}
.ub_jt {
  width: 16rpx;
  height: 25rpx;
  display: block;
  float: right;
  margin: 32rpx 30rpx 0 0;
}

.addi21 {
  width: 50%;
  float: left;
}
.addi21 text {
  width: 100rpx;
}
.addi21 input {
  width: 250rpx;
  float: left;
  margin: 20rpx 0 0 0;
}
</style>
